package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.ProductSchemeConfig;
import com.qiantu.api.entity.SchemeConfig;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.ui.adapter.DeviceComposeAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import okhttp3.Call;

public class DeviceComposeActivity extends AppActivity {
    private RecyclerView composeView;
    private DeviceComposeAdapter adapter;
    private SchemeConfig schemeConfig;
    @Log
    public static void start(Context context, String controllerSerialNo) {
        Intent intent = new Intent(context, DeviceComposeActivity.class);
        intent.putExtra("controllerSerialNo", controllerSerialNo);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_compose;
    }

    @Override
    public void initView() {
        composeView = findViewById(R.id.recycel_view_device_compose);
        composeView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new DeviceComposeAdapter(getContext()){
            @Override
            public void onSelectChanged(int selected) {
                super.onSelectChanged(selected);
            }
        };
        composeView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        LLHttpManager.l8dcGetCompose(this,getIntent().getStringExtra("controllerSerialNo"),new HttpCallback<HttpData<SchemeConfig>>(null) {
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<SchemeConfig> result) {
                schemeConfig = result.getData();
                List<ProductSchemeConfig> composeLists = schemeConfig.getProductSchemeConfigs();
                adapter.setSchemeValue(schemeConfig.getSchemeValue());
                adapter.setSelectPosition(1);
                adapter.setData(composeLists);
                hideDialog();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {

            }
        });
    }

    @Override
    public void onRightClick(View view) {
        int position = adapter.getSelectPosition();
        ProductSchemeConfig config = schemeConfig.getProductSchemeConfigs().get(position);
        LLHttpManager.l8dcSetCompose(this,getIntent().getStringExtra("controllerSerialNo"), config.getProductSchemeConfigSerialNo(),new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                getControllerData();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {

            }
        });
    }

    public void getControllerData(){
        LLHttpManager.getControllers(this, AppApplication.getInstance().getCurrentHouseSerialNo(),new HttpCallback<HttpData<List<ControllerBean>>>(null) {
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<List<ControllerBean>> result) {
                LLControllerDBManager.getInstance(getContext()).refreshAllControllersTx(result.getData());
                getDeviceData();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {

            }
        });
    }

    public void getDeviceData(){
        LLHttpManager.house_getdevices(new ApplicationLifecycle(), AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<List<DeviceBean>>>(this) {
            @Override
            public void onSucceed(HttpData<List<DeviceBean>> result) {
                super.onSucceed(result);
                LLDeviceDBManager.getInstance(getContext()).refreshAllDevicesTx( result.getData(), null);
                EventBus.getDefault().post(new RefreshDeviceListEvent());
                finish();
                hideDialog();
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }
        });
    }
}
